<template>
  <div class="app-container">
    <el-row style="margin-bottom: 0">
      <!-- 查询表单 -->
      <el-col :span="19">
        <el-form :inline="true" size="medium">
          <el-form-item label="图书">
            <el-input
              clearable
              v-model="searchObj.isbn"
              class="inline-input"
              placeholder="ISBN书号"
              value-key="no"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              clearable
              v-model="searchObj.name"
              class="inline-input"
              placeholder="图书名"
              value-key="name"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item label="员工">
            <el-input
              clearable
              v-model="searchObj.no"
              class="inline-input"
              placeholder="工号"
              value-key="no"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              clearable
              v-model="searchObj.employeeName"
              class="inline-input"
              placeholder="姓名"
              value-key="employeeName"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <el-table
          :data="list"
          :header-cell-style="{'text-align':'center'}"
          fit
          stripe
          tooltip-effect="light"
          v-loading="loading"
          highlight-current-row
          element-lodaing-text="数据加载中"
        >
          <el-table-column type="index" label="#" align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="归还状态" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 1" type="primary">已归还</el-tag>
              <el-tag v-if="scope.row.status === 2" type="warning">未归还</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="isbn" label="ISBN书号" sortable align="center"/>
          <el-table-column prop="bookName" label="图书名" sortable align="center"/>
          <el-table-column prop="no" label="员工号" sortable align="center"/>
          <el-table-column prop="employeeName" label="员工姓名" sortable align="center"/>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { borrowApi } from '@/api'

export default {
  // 定义数据模型
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 图书列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}// 查询条件
    }
  },
  computed: {},
  // 页面渲染后获取数据
  created() {
    this.fetchData()
  },
  // 定义方法
  methods: {
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      borrowApi.queryByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    // 更改每页记录数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 更改页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 修改
    edit(id) {
      this.$router.push('/book/edit/' + id)
    },
    // 删除
    removeById(id) {
      this.$confirm('将会删除这条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return borrowApi.deleteById(id)
      }).then(() => {
        this.fetchData()
        this.$message.success('删除成功')
      }).catch(error => {
        // 当取消时会进入catch语句error = 'cancel'
        // 当后端服务抛出异常时error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";
</style>
